//
//  BRPopoverView.m
//  BrezzeSaaS
//
//  Created by IOS on 2022/1/12.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRPopoverView.h"

@interface BRPopoverView ()
@property (nonatomic, strong) UILabel *textLB;
@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UIImageView *triangleImg;
@property (nonatomic, strong) CAShapeLayer *triangleLayer;
@end

@implementation BRPopoverView


- (UILabel *)textLB
{
    if (_textLB == nil) {
        _textLB = [[UILabel alloc] initWithFrame:CGRectZero];
//        _textLB.backgroundColor = UIColorMakeWithRGBA(0, 0, 0, 0.7);
        _textLB.text = Localized(@"Login_agreeToast");
        _textLB.textAlignment = NSTextAlignmentLeft;
        _textLB.numberOfLines = 0;
        _textLB.textColor = UIColorWhite;
        _textLB.font = [UIFont fontR:12];
    }
    return _textLB;
}

- (UIView *)bgView {
    
    if (_bgView == nil) {
        _bgView = [[UIView alloc] init];
        _bgView.layer.cornerRadius = 12.;
        _bgView.layer.masksToBounds = YES;
        _bgView.backgroundColor = UIColorMakeWithHex(@"#AAAAAC");
        
    }
    return _bgView;
}

- (UIImageView *)triangleImg {
    if (_triangleImg == nil) {
        _triangleImg = [[UIImageView alloc] initWithImage:UIImageMake(@"login_triangle")];
    }
    return _triangleImg;
}

- (void)br_remove {
    if (self.superview) {
        [self removeFromSuperview];
    }
}

- (void)br_add:(UIView *)relateView direction:(BOOL)isDown {
    
    //35
    [self addSubview:self.triangleImg];
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.textLB];
    [self.textLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(6);
        make.bottom.offset(-6);
        make.left.offset(16);
        make.right.offset(-16);
    }];
    if (isDown) {
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(self);
            make.top.equalTo(self).offset(4);
        }];
        
        [self.triangleImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(relateView.mas_centerX);
            make.bottom.equalTo(self.bgView.mas_top);
            make.width.mas_equalTo(6);
            make.height.mas_equalTo(4);
        }];
        
        return;
    }
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.bottom.equalTo(self).offset(-4);
    }];
    
    [self.triangleImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(relateView.mas_centerX);
        make.top.equalTo(self.bgView.mas_bottom);
        make.width.mas_equalTo(6);
        make.height.mas_equalTo(4);
    }];
    self.triangleImg.transform =  CGAffineTransformMakeRotation(M_PI);
}

- (void)addTriangleRelateView:(UIView *)view
{
    if (_triangleLayer == nil) {
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(view.mj_w / 2.0 - 8, view.mj_h)];
        [path addLineToPoint:CGPointMake(view.mj_w  / 2.0, view.mj_h + 10)];
        [path addLineToPoint:CGPointMake(view.mj_w  / 2.0 + 8, view.mj_h)];
        _triangleLayer = [CAShapeLayer layer];
        _triangleLayer.path = path.CGPath;
        _triangleLayer.fillColor = view.backgroundColor.CGColor;
        [self.layer addSublayer:_triangleLayer];
    }
}

- (void)adjustBoundsWithMaxWidth:(CGFloat)maxWidth relateView:(UIView *)relateView
{
    [_textLB sizeToFit];
    // 最大宽度，防止超出屏幕
    if (_textLB.mj_w >=maxWidth) {
        _textLB.mj_w = maxWidth;
    }
    [_textLB sizeToFit];
    _textLB.mj_w += 10;
    _textLB.mj_h += 10;
    _textLB.layer.cornerRadius = 8;
    _textLB.layer.masksToBounds = YES;
    
    self.mj_w = _textLB.mj_w;
    self.mj_h = _textLB.mj_h;
    //
    [self addSubview:self.textLB];
    [self addTriangleRelateView:self.textLB];
}

+ (instancetype)showText:(NSString *)text inView:(UIView *)superView relateView:(UIView *)relateView
{
    return [[self class] showText:text inView:superView relateView:relateView direction:NO];
}

+ (instancetype)showText:(NSString *)text inView:(UIView *)superView relateView:(UIView *)relateView direction:(BOOL)isDown {
    
    BRPopoverView *popoverView = [[BRPopoverView alloc] init];
    popoverView.backgroundColor = UIColorClear;
    popoverView.textLB.text = text;
    [superView addSubview:popoverView];
    [popoverView br_add:relateView direction:isDown];
    
    if (isDown) {
        [popoverView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_lessThanOrEqualTo(kScreenWidth - 40);
            make.top.equalTo(relateView.mas_bottom).offset(0);
            make.left.equalTo(superView).offset(20);
        }];
    }else {
        [popoverView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(kScreenWidth - 40);
            make.bottom.equalTo(relateView.mas_top).offset(0);
            make.left.equalTo(superView).offset(20);
        }];
    }
    
   
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [popoverView br_remove];
    });
    
    return popoverView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

